package entity

import (
	"errors"
	"fmt"
	"math"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
	"shqsoft.cn/pas3ui/workflow"
	"shqsoft.cn/pas3ui/workflow/entity"
)

//CompZsWorkflowNewApp
type CompZsWorkflowNewApp struct {
	ID       int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	JLID     int       `gorm:"column:JLID"`
	JYRQ     time.Time `gorm:"column:JYRQ"`
	ITNO     string    `gorm:"column:ITNO"`
	ITNM     string    `gorm:"column:ITNM"`
	CUNM     string    `gorm:"column:CUNM"`
	CYNM     string    `gorm:"column:CYNM"`
	ZSJE     float64   `gorm:"column:ZSJE"`
	EMNO1    string    `gorm:"column:EMNO1"`
	EMID1    int       `gorm:"column:EMID1"`
	EMNM1    string    `gorm:"-" json:"emnm1"`
	DSVL1    float64   `gorm:"column:DSVL1"`
	EMNO2    string    `gorm:"column:EMNO2"`
	EMNM2    string    `gorm:"-" json:"emnm2"`
	EMID2    int       `gorm:"column:EMID2"`
	DSVL2    float64   `gorm:"column:DSVL2"`
	EMNO3    string    `gorm:"column:EMNO3"`
	EMNM3    string    `gorm:"-" json:"emnm3"`
	EMID3    int       `gorm:"column:EMID3"`
	DSVL3    float64   `gorm:"column:DSVL3"`
	EMNO4    string    `gorm:"column:EMNO4"`
	EMNM4    string    `gorm:"-" json:"emnm4"`
	EMID4    int       `gorm:"column:EMID4"`
	DSVL4    float64   `gorm:"column:DSVL4"`
	EMNO5    string    `gorm:"column:EMNO5"`
	EMNM5    string    `gorm:"-" json:"emnm5"`
	EMID5    int       `gorm:"column:EMID5"`
	DSVL5    float64   `gorm:"column:DSVL5"`
	EMNO6    string    `gorm:"column:EMNO6"`
	EMNM6    string    `gorm:"-" json:"emnm6"`
	EMID6    int       `gorm:"column:EMID6"`
	DSVL6    float64   `gorm:"column:DSVL6"`
	EMNO7    string    `gorm:"column:EMNO7"`
	EMNM7    string    `gorm:"-" json:"emnm7"`
	EMID7    int       `gorm:"column:EMID7"`
	DSVL7    float64   `gorm:"column:DSVL7"`
	EMNO8    string    `gorm:"column:EMNO8"`
	EMNM8    string    `gorm:"-" json:"emnm8"`
	EMID8    int       `gorm:"column:EMID8"`
	DSVL8    float64   `gorm:"column:DSVL8"`
	EMNO9    string    `gorm:"column:EMNO9"`
	EMNM9    string    `gorm:"-" json:"emnm9"`
	EMID9    int       `gorm:"column:EMID9"`
	DSVL9    float64   `gorm:"column:DSVL9"`
	EMNO10   string    `gorm:"column:EMNO10"`
	EMNM10   string    `gorm:"-" json:"emnm10"`
	EMID10   int       `gorm:"column:EMID10"`
	DSVL10   float64   `gorm:"column:DSVL10"`
	EMNO11   string    `gorm:"column:EMNO11"`
	EMNM11   string    `gorm:"-" json:"emnm11"`
	EMID11   int       `gorm:"column:EMID11"`
	DSVL11   float64   `gorm:"column:DSVL11"`
	EMNO12   string    `gorm:"column:EMNO12"`
	EMNM12   string    `gorm:"-" json:"emnm12"`
	EMID12   int       `gorm:"column:EMID12"`
	DSVL12   float64   `gorm:"column:DSVL12"`
	EMNO13   string    `gorm:"column:EMNO13"`
	EMNM13   string    `gorm:"-" json:"emnm13"`
	EMID13   int       `gorm:"column:EMID13"`
	DSVL13   float64   `gorm:"column:DSVL13"`
	EMNO14   string    `gorm:"column:EMNO14"`
	EMNM14   string    `gorm:"-" json:"emnm14"`
	EMID14   int       `gorm:"column:EMID14"`
	DSVL14   float64   `gorm:"column:DSVL14"`
	EMNO15   string    `gorm:"column:EMNO15"`
	EMNM15   string    `gorm:"-" json:"emnm15"`
	EMID15   int       `gorm:"column:EMID15"`
	DSVL15   float64   `gorm:"column:DSVL15"`
	UID      int       `gorm:"column:UID"`
	FRID     int       `gorm:"column:FRID"`
	WFID     int       `gorm:"column:WFID"`
	SQR      string    `gorm:"column:SQR"`
	SQDT     time.Time `gorm:"column:SQDT"`
	STEP     int       `gorm:"-" json:"step"`
	Progress float64   `gorm:"-" json:"progress"`
	StepName string    `gorm:"-" json:"stepName"`
	Status   string    `gorm:"-" json:"status"`
	TPID     int       `gorm:"-" json:"tpid"`
}

//TableName
func (CompZsWorkflowNewApp) TableName() string {
	return "T_COMP_ZSCLIE_DIVI"
}

//BeforeSave 保存前的触发器
func (i *CompZsWorkflowNewApp) BeforeSave(tx *gorm.DB) (err error) {
	if i.JLID > 0 {
		sqlEx := `SELECT "DATA_DT","ITNO","ITNM","CUNM","CYNM","ZSJE","TPID" FROM "V_CM_BIZ_FEE_SUM_M_NEW" WHERE "JLID" = ?`
		row := base.CHDB.Raw(sqlEx, i.JLID).Row()
		row.Scan(&i.JYRQ, &i.ITNO, &i.ITNM, &i.CUNM, &i.CYNM, &i.ZSJE, &i.TPID)
	} else {
		sqlEx := `SELECT "JLID","TPID" FROM "V_CM_BIZ_FEE_SUM_M_NEW" WHERE "DATA_DT" = ? AND "ITNO"=? AND "ITNM"=? AND "CUNM"=? AND "CYNM"=? AND "ZSJE"=?`
		row := base.CHDB.Raw(sqlEx, i.JYRQ, i.ITNO, i.ITNM, i.CUNM, i.CYNM, i.ZSJE).Row()
		row.Scan(&i.JLID, &i.TPID)
	}
	if i.JLID == 0 {
		return errors.New("交易日期，科目，币种，客户名称，中收金额不存在或不匹配！")
	}
	bdate, err := base.GetBackDATE()
	if err != nil {
		return
	}
	if i.JYRQ.Unix() < bdate.Unix() {
		err = fmt.Errorf("分户日期【" + i.JYRQ.Format("2006-01-02") + "】不能小于最早可追溯日期，最大追溯日期为【" + bdate.Format("2006-01-02") + "】")
		return
	}
	sum := i.DSVL1 + i.DSVL2 + i.DSVL3 + i.DSVL4 + i.DSVL5 + i.DSVL6 + i.DSVL7 + i.DSVL8 + i.DSVL9 + i.DSVL10 + i.DSVL11 + i.DSVL12 + i.DSVL13 + i.DSVL14 + i.DSVL15
	if math.Abs(sum-i.ZSJE) > 0.1 {
		return fmt.Errorf("分配金额之和必须等于总金额%.2f", i.ZSJE)
	}
	ec := base.GetEmployeeCache()
	ec.GetIdByNo(i.EMNO1)
	i.EMID1, err = ec.GetIdByNo(i.EMNO1)
	if err != nil || i.EMID1 == 0 {
		err = fmt.Errorf("员工工号1:%s在系统中没有找到！", i.EMNO1)
		return
	}
	i.EMNM1, _ = ec.GetNameByID(i.EMID1)
	if i.UID == 0 {
		i.UID = i.EMID1
	}
	if i.DSVL2 > 0 {
		ec.GetIdByNo(i.EMNO2)
		i.EMID2, err = ec.GetIdByNo(i.EMNO2)
		if err != nil || i.EMID2 == 0 {
			err = fmt.Errorf("员工工号2:%s在系统中没有找到！", i.EMNO2)
			return
		}
		i.EMNM2, _ = ec.GetNameByID(i.EMID2)
	}
	if i.DSVL3 > 0 {
		ec.GetIdByNo(i.EMNO3)
		i.EMID3, err = ec.GetIdByNo(i.EMNO3)
		if err != nil || i.EMID3 == 0 {
			err = fmt.Errorf("员工工号3:%s在系统中没有找到！", i.EMNO3)
			return
		}
		i.EMNM3, _ = ec.GetNameByID(i.EMID3)
	}
	if i.DSVL4 > 0 {
		ec.GetIdByNo(i.EMNO4)
		i.EMID4, err = ec.GetIdByNo(i.EMNO4)
		if err != nil || i.EMID4 == 0 {
			err = fmt.Errorf("员工工号4:%s在系统中没有找到！", i.EMNO4)
			return
		}
		i.EMNM4, _ = ec.GetNameByID(i.EMID4)
	}
	if i.DSVL5 > 0 {
		ec.GetIdByNo(i.EMNO5)
		i.EMID5, err = ec.GetIdByNo(i.EMNO5)
		if err != nil || i.EMID5 == 0 {
			err = fmt.Errorf("员工工号4:%s在系统中没有找到！", i.EMNO5)
			return
		}
		i.EMNM5, _ = ec.GetNameByID(i.EMID5)
	}
	if i.DSVL6 > 0 {
		ec.GetIdByNo(i.EMNO6)
		i.EMID6, err = ec.GetIdByNo(i.EMNO6)
		if err != nil || i.EMID6 == 0 {
			err = fmt.Errorf("员工工号4:%s在系统中没有找到！", i.EMNO6)
			return
		}
		i.EMNM6, _ = ec.GetNameByID(i.EMID6)
	}
	if i.DSVL7 > 0 {
		ec.GetIdByNo(i.EMNO7)
		i.EMID7, err = ec.GetIdByNo(i.EMNO7)
		if err != nil || i.EMID7 == 0 {
			err = fmt.Errorf("员工工号7:%s在系统中没有找到！", i.EMNO7)
			return
		}
		i.EMNM7, _ = ec.GetNameByID(i.EMID7)
	}
	if i.DSVL8 > 0 {
		ec.GetIdByNo(i.EMNO8)
		i.EMID8, err = ec.GetIdByNo(i.EMNO8)
		if err != nil || i.EMID8 == 0 {
			err = fmt.Errorf("员工工号8:%s在系统中没有找到！", i.EMNO8)
			return
		}
		i.EMNM8, _ = ec.GetNameByID(i.EMID8)
	}
	if i.DSVL9 > 0 {
		ec.GetIdByNo(i.EMNO9)
		i.EMID9, err = ec.GetIdByNo(i.EMNO9)
		if err != nil || i.EMID9 == 0 {
			err = fmt.Errorf("员工工号9:%s在系统中没有找到！", i.EMNO9)
			return
		}
		i.EMNM9, _ = ec.GetNameByID(i.EMID9)
	}
	if i.DSVL10 > 0 {
		ec.GetIdByNo(i.EMNO10)
		i.EMID10, err = ec.GetIdByNo(i.EMNO10)
		if err != nil || i.EMID10 == 0 {
			err = fmt.Errorf("员工工号10:%s在系统中没有找到！", i.EMNO10)
			return
		}
		i.EMNM10, _ = ec.GetNameByID(i.EMID10)
	}
	if i.DSVL11 > 0 {
		ec.GetIdByNo(i.EMNO11)
		i.EMID11, err = ec.GetIdByNo(i.EMNO11)
		if err != nil || i.EMID11 == 0 {
			err = fmt.Errorf("员工工号10:%s在系统中没有找到！", i.EMNO11)
			return
		}
		i.EMNM11, _ = ec.GetNameByID(i.EMID11)
	}
	if i.DSVL12 > 0 {
		ec.GetIdByNo(i.EMNO12)
		i.EMID12, err = ec.GetIdByNo(i.EMNO12)
		if err != nil || i.EMID12 == 0 {
			err = fmt.Errorf("员工工号10:%s在系统中没有找到！", i.EMNO12)
			return
		}
		i.EMNM12, _ = ec.GetNameByID(i.EMID12)
	}
	if i.DSVL13 > 0 {
		ec.GetIdByNo(i.EMNO13)
		i.EMID13, err = ec.GetIdByNo(i.EMNO13)
		if err != nil || i.EMID13 == 0 {
			err = fmt.Errorf("员工工号10:%s在系统中没有找到！", i.EMNO13)
			return
		}
		i.EMNM13, _ = ec.GetNameByID(i.EMID13)
	}
	if i.DSVL14 > 0 {
		ec.GetIdByNo(i.EMNO14)
		i.EMID14, err = ec.GetIdByNo(i.EMNO14)
		if err != nil || i.EMID14 == 0 {
			err = fmt.Errorf("员工工号10:%s在系统中没有找到！", i.EMNO14)
			return
		}
		i.EMNM14, _ = ec.GetNameByID(i.EMID14)
	}
	if i.DSVL15 > 0 {
		ec.GetIdByNo(i.EMNO15)
		i.EMID15, err = ec.GetIdByNo(i.EMNO15)
		if err != nil || i.EMID15 == 0 {
			err = fmt.Errorf("员工工号10:%s在系统中没有找到！", i.EMNO15)
			return
		}
		i.EMNM15, _ = ec.GetNameByID(i.EMID15)
	}
	if i.ID == 0 {
		var count int
		sqlStr := `select count(a."ID") from "T_COMP_ZSCLIE_DIVI" a inner join "T_FLOW_RUN" b on a."FRID"=b."ID" where b."STATE"='0' and a."JLID"=?`
		row := tx.Raw(sqlStr, i.JLID).Row()
		row.Scan(&count)
		if count > 0 {
			return fmt.Errorf("客户:%s已经在申请中，不能重复申请！", i.CUNM)
		}
	}
	if i.ID > 0 {
		row := base.MYDB.Model(CompZsWorkflowNewApp{}).Where(`"ID"=?`, i.ID).Select(`"FRID"`).Row()
		row.Scan(&i.FRID)
	}

	if i.FRID == 0 {
		ei, _ := workflow.GetEntityInfo("CompZsWorkflowNewApp")
		i.WFID, err = ei.WfidFunc(i)
		if err != nil {
			return err
		}
		if i.WFID == 0 {
			return errors.New("获取新中收申请WFID失败")
		}
		runid, err := workflow.Create(i.WFID, i.UID, tx)
		if err != nil {
			return err
		}
		i.FRID = runid
	}
	i.SQDT = time.Now()
	return
}

// AfterFind 查询后触发器
func (i *CompZsWorkflowNewApp) AfterFind(tx *gorm.DB) (err error) {
	ec := base.GetEmployeeCache()
	i.EMNM1, err = ec.GetNameByID(i.EMID1)
	if err != nil {
		return err
	}
	if i.DSVL2 > 0 {
		i.EMNM2, err = ec.GetNameByID(i.EMID2)
		if err != nil {
			return err
		}
	}
	if i.DSVL3 > 0 {
		i.EMNM3, err = ec.GetNameByID(i.EMID3)
		if err != nil {
			return err
		}
	}
	if i.DSVL4 > 0 {
		i.EMNM4, err = ec.GetNameByID(i.EMID4)
		if err != nil {
			return err
		}
	}
	if i.DSVL5 > 0 {
		i.EMNM5, err = ec.GetNameByID(i.EMID5)
		if err != nil {
			return err
		}
	}
	if i.DSVL6 > 0 {
		i.EMNM6, err = ec.GetNameByID(i.EMID6)
		if err != nil {
			return err
		}
	}
	if i.DSVL7 > 0 {
		i.EMNM7, err = ec.GetNameByID(i.EMID7)
		if err != nil {
			return err
		}
	}
	if i.DSVL8 > 0 {
		i.EMNM8, err = ec.GetNameByID(i.EMID8)
		if err != nil {
			return err
		}
	}
	if i.DSVL9 > 0 {
		i.EMNM9, err = ec.GetNameByID(i.EMID9)
		if err != nil {
			return err
		}
	}
	if i.DSVL10 > 0 {
		i.EMNM10, err = ec.GetNameByID(i.EMID10)
		if err != nil {
			return err
		}
	}
	if i.DSVL11 > 0 {
		i.EMNM11, err = ec.GetNameByID(i.EMID11)
		if err != nil {
			return err
		}
	}
	if i.DSVL12 > 0 {
		i.EMNM12, err = ec.GetNameByID(i.EMID12)
		if err != nil {
			return err
		}
	}
	if i.DSVL13 > 0 {
		i.EMNM13, err = ec.GetNameByID(i.EMID13)
		if err != nil {
			return err
		}
	}
	if i.DSVL14 > 0 {
		i.EMNM14, err = ec.GetNameByID(i.EMID14)
		if err != nil {
			return err
		}
	}
	if i.DSVL15 > 0 {
		i.EMNM15, err = ec.GetNameByID(i.EMID15)
		if err != nil {
			return err
		}
	}
	if i.FRID == 0 {
		i.Status = "exception"
		i.StepName = "校验错误"
		return
	}
	run := entity.FlowRun{}
	if err = tx.Find(&run, i.FRID).Error; err != nil {
		tx.Rollback()
		return
	}
	wf := workflow.GetWorkFlow(run.WFID)
	if wf == nil {
		return fmt.Errorf("指定的流程ID:%d在流程配置中不存在！", run.WFID)
	}
	act := wf.GetAction(run.Step)
	if act == nil {
		return fmt.Errorf("指定的stepID:%d在流程%s配置中不存在！", run.Step, wf.Name)
	}
	count := len(wf.Actions)
	i.STEP = run.Step
	i.StepName = "审批中"
	if i.STEP == 1 {
		i.StepName = "申请中"
	}
	i.Progress = float64(act.Step*100) / float64(count)
	if i.Progress >= 100 && run.State == "0" {
		i.Progress = 95
	}
	i.Status = ""
	if run.State == "0" {
		i.Status = "active"
	} else if run.State == "2" {
		i.StepName = "已中止"
		i.Status = "exception"
	} else {
		i.StepName = "已完成"
		i.Status = "success"
	}
	return
}

func (i *CompZsWorkflowNewApp) AfterDelete(tx *gorm.DB) (err error) {
	if i.FRID > 0 {
		return workflow.DeleteWithTx(i.FRID, tx)
	}
	return nil
}

func CompZsWorkflowNewAppEF() base.EntityFunc {
	return func() interface{} {
		entity := CompZsWorkflowNewApp{}
		return &entity
	}
}
func CompZsWorkflowNewAppESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []CompZsWorkflowNewApp
		return &entities
	}
}

func getCompZsNewWfidSh(params interface{}) (wfid int, err error) {
	obj, ok := params.(*CompZsWorkflowNewApp)
	if !ok {
		err = errors.New("参数类型必须是int类型的中收类型ID")
		return
	}
	if obj.TPID == 4 {
		wfid = 73
	} else if obj.TPID == 5 {
		wfid = 72
	} else {
		wfid = 71
	}
	return
}
func getCompZsNewWfidSz(params interface{}) (wfid int, err error) {
	return
}

func init() {
	workflow.SetWfidFunc("getCompZsNewWfidSh", getCompZsNewWfidSh)
	workflow.SetWfidFunc("getCompZsNewWfidSz", getCompZsNewWfidSz)
	ei, err := workflow.GetEntityInfo("CompZsWorkflowNewApp")
	if err != nil {
		panic(err)
	}
	dao := base.NewDefaultDao(CompZsWorkflowNewAppEF(), CompZsWorkflowNewAppESF(), nil, "my")
	filter := base.WorkFolowFilter{
		FilterColName: "FRID",
	}
	filter.SetAdditional("wfid", ei.WfidsToString())
	filter.SetAdditional("manager", ei.MgMapToString())
	dao.Fileter = &filter
	dao.Fileter.SetDB("my")
	base.DaoPoolObj.Put("CompZsWorkflowNewApp", dao)
}
